<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

class QQ
{
    protected $_config = null;
    
    public function __construct() {
        $this->_config = Loader::loadConfig('qq', 'admin');
    }
    
    public function qq_login($inadmin = false) {
        $callback = urlencode($this->_config['callback'].($inadmin ? '&inadmin=1' : ''));
        $_SESSION['state'] = md5(uniqid(rand(), true));
        $login_url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' 
            . $this->_config['appid'] . '&redirect_uri=' . $callback
            . '&state=' . $_SESSION['state']
            . '&scope=' . $this->_config['scope'];

        header('Location:'.$login_url);
    }
    
    public function do_post($url, $data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        curl_setopt($ch, CURLOPT_POST, TRUE); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
        curl_setopt($ch, CURLOPT_URL, $url);
        $ret = curl_exec($ch);
    
        curl_close($ch);
        return $ret;
    }
    
    public function get_url_contents($url) {
        if (ini_get('allow_url_fopen') == '1') {
            //依赖与openssl扩展
            return file_get_contents($url);
        }
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result =  curl_exec($ch);
        curl_close($ch);
    
        return $result;
    }
    
    public function qq_callback() {
        if($_REQUEST['state'] ==  $_SESSION['state']) {
            $token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'
                . 'client_id=' . $this->_config['appid']. '&redirect_uri=' . urlencode($this->_config['callback'])
                . '&client_secret=' . $this->_config['appkey']. '&code=' . $_REQUEST['code'];

            $response = $this->get_url_contents($token_url);
            if (strpos($response, 'callback') !== false) {
                $lpos = strpos($response, '(');
                $rpos = strrpos($response, ')');
                $response  = substr($response, $lpos + 1, $rpos - $lpos -1);
                $msg = json_decode($response);
                if (isset($msg->error)) {
                    echo '<h3>error:</h3>' . $msg->error;
                    echo '<h3>msg  :</h3>' . $msg->error_description;
                    exit;
                }
            }
    
            $params = array();
            parse_str($response, $params);
    
            $_SESSION['qq'] = $params['access_token'];
        }
        
        unset($_SESSION['state']);
        return isset($_SESSION['qq']) ? $_SESSION['qq'] : '';
    }
    
    public function get_openid($access_token) {
        $graph_url = 'https://graph.qq.com/oauth2.0/me?access_token=' . $access_token;
    
        $str  = $this->get_url_contents($graph_url);
        if (strpos($str, 'callback') !== false) {
            $lpos = strpos($str, '(');
            $rpos = strrpos($str, ')');
            $str  = substr($str, $lpos + 1, $rpos - $lpos -1);
        }
    
        $user = json_decode($str);
        if (isset($user->error)) {
            echo '<h3>error:</h3>' . $user->error;
            echo '<h3>msg  :</h3>' . $user->error_description;
            exit;
        }
        
        $_SESSION['qq_openid'] = $user->openid;
        return $user->openid;
    }
    
    //获得空间资料
    public function get_space_user_info() {
        $url = 'https://graph.qq.com/user/get_user_info?'
            . 'access_token=' . $_SESSION['qq']
            . '&oauth_consumer_key=' . $this->_config['appid']
            . '&openid=' . $_SESSION['qq_openid']
            . '&format=json';
        
        $r = json_decode($this->get_url_contents($url), true);
        if($r['ret'] != 0) {
            return false;
        }
        
        return $r;
    }

    public function get_weibo_user_info() {
        $url = 'https://graph.qq.com/user/get_info?'
        . 'access_token=' . $_SESSION['qq']
        . '&oauth_consumer_key=' . $this->_config['appid']
        . '&openid=' . $_SESSION['qq_openid']
        . '&format=json';
        
        $r = json_decode($this->get_url_contents($url), true);
        if($r['ret'] != 0) {
            return false;
        }
        
        return $r;
    }
    
    //根据需要自行添加所需方法
}